

exports.modifyMember = async function(req,res,next) {
  if( isEmpty(req.body) ) {
    return res.resend.sendJson(req, res, {
      status: 400,
      code: 'FAIL',
      data: {
        token: req.newToken
      },
      msg: 'id不能为空'
    })
  }
  try {
      const values = Object.assign( {}, req.body, {
        connection: req.connection
      })
        const [ modifyMember ] =  await exeModifySql(values)
        if( modifyMember.affectedRows > 0) {
          return res.resend.sendJson(req, res, {
            status: 200,
            code: 'SUCCESS',
            msg: '修改成功',

            data: {
              token: req.newToken
            }
          })
        }
        res.resend.sendJson(req, res, {
          status: 200,
          data: {
            token: req.newToken
          },
          code: 'FAIL',
          msg: '没有相应的数据可被修改'
        })

      }
      catch(err) {
       return  res.resend.sendJson(req, res, {
        status: 500,
        data: '',
        code: 'FAIL',
        msg: err.message,
        data: '',
        logMsg: err
       })
      }
}

// 参数id判空
function isEmpty(params) {
  const {
    id
  } = params;
  return !id ? true : false;
}



//依据id查询是否有对应的数据
function getMember(params) {
  return new Promise( async(resolve, reject) => {
    try {
      const { id, connection } = params;
      const sql = `SELECT * FROM members WHERE id = ${id}`;
      const [result, fie] = await connection.query(sql, []);
      resolve([result, fie])
    }
    catch(err) {
      reject(err)
    }
  })
}

// 修改会员信息
function  exeModifySql(member = {}) {
  return new Promise( async(resolve, reject) => {
    try {
      const sql = `UPDATE members SET userName = ?, phone = ?, projectName = ?, money = ?, age = ?, occupatuin = ?, address = ? WHERE id = ?`;
      const result = await member.connection.query(sql, [member.userName, member.phone, member.projectName, member.money, member.age, member.occupatuin, member.address, member.id]);
     
      resolve(result)
    }
    catch(err) {
      reject(err)
    }
  })
}